Search algorithm Clever algorithm even for a single word Example: find abac in abaababac See Knuth-Morris-Pratt and String searching algorithm on wikipedia
2 Subset construction We have defined for a DFA L(A) = {x Σ ˆδ(q 0, x) F } and for A NFA L(A) = {x Σ ˆδ(q 0, x) F } For any NFA A we can build a DFA A D such that L(A) = L(A D )
3 Regular languages Given an alphabet Σ, a language L Σ is regular iff there exists a DFA A such that L = L(A) Theorem: A language L is regular iff there exists a NFA N such that L = L(N) Proof: If L is regular then L = L(A) for some DFA A. To any DFA A we can associate a NFA N A such that L(A) = L(N A ). If A = (Q, Σ, δ, q 0, F) we simply take N A = (Q, Σ, δ, q 0, F) with δ (q, a) = {δ(q, a)}. Notice that δ Q Σ Pow(Q). In the other direction, if L = L(N) for some NFA N then, the power set construction gives a DFA A such that L(N) = L(A). We have then L = L(A) and so L is regular. Q.E.D.
4 Automata with -Transitions Another extension of the notion of automata that is useful but adds no more power Intuitively an -transition occurs when one can go from one state to another without reading any input symbol choc 0 5 kr stop A vending machine that may decide to stop
5 Σ = {b} Automata with -Transitions 2 3 b b b 4 5 6 -NFA accepting {b,bb,bbb} The machine can jump by itself from the state to the state 2
6 Automata with -Transitions Example: decimal numbers consisting of. An optional + or - sign 2. A string of digits 3. A decimal point, and 4. Another string of digits. Either this string, or the string (2) can be empty, but at least one of them is nonempty.
7 Automata with -Transitions A possible -NFA for this language is 0,,...,9 0,,...,9 A,+, B C 0,,...,9 D 0,,...,9 Notice the crucial use of transition to represent the optional choice of the sign + or - E
8 Automata with -Transitions Definition A -NFA consists of. a finite set of states (often denoted Q) 2. a finite set Σ of symbols (alphabet) 3. a transition function that takes as argument a state and an element of Σ {} and returns a set of states (often denoted δ); this set can be empty 4. a start state 5. a set of final or accepting states (often denoted F) We have F Q and δ Q (Σ {}) Pow(Q)
9 Automata with -Transitions For the example of decimal numbers the transition table is +,- 0,,...,9 A {B} {B} B {C} {B, E} C {D} D {D} E {D}
0 -Closures If X Q we define the -closure ECLOSE(X) inductively BASIS: If q X then q is in ECLOSE(X) INDUCTION: If p is in ECLOSE(X) and r δ(p, ) then r is in ECLOSE(X) Note that ECLOSE( ) = Informally, we follow all transitions out of X that are labeled. We say that X is -closed iff X = ECLOSE(X). Remark: X is -closed iff q X and q q implies q X
-Closures Yet another way to present ECLOSE(X) is with the two rules q X q ECLOSE(X) q ECLOSE(X) q δ(q, ) q ECLOSE(X) Intuitively q ECLOSE(X) iff there exists q 0 X and a sequence of -transitions q 0 q... qn = q
2 -Closures We say that Y Q is -closed iff If q in Y and q in δ(q, ) then q in Y We have that ECLOSE(X) is the smallest subset of Q containing X which is -closed
3 For the automaton -Closures B C F A b D a E G we have ECLOSE({A}) = {A,B,C,D,F}
4 Functional representation import List(union) data Q = A B C D E F G deriving (Eq,Show) jump :: Q -> [Q] jump A = [B,D] jump B = [C] jump C = [F] jump F = [] jump D = [] jump E = [G]
5 Functional representation issub as bs = and (map (\x -> elem x bs) as) isclos as = issub (as >>= jump) as closure qs = let qs = qs >>= jump in if issub qs qs then qs else closure (union qs qs )
6 How to run an -NFA Given any -NFA E = (Q, Σ, δ, q 0, F) we define ˆδ(q, ) = ECLOSE({q}) ˆδ(q, ay) = ˆδ(p, p (ECLOSE(q),a) y) where (X, a) = q X δ(q, a) Definition: L(E) = {x Σ ˆδ(q 0, x) F } Remark: All sets q.x = ˆδ(q, x) are -closed Remark: q.a is ECLOSE( (ECLOSE(q),a))
7 Representation in functional programming import List(union) data Q = A B C D E deriving (Eq,Show) jump :: Q -> [Q] jump A = [B] jump B = [] jump C = [] jump D = [] jump E = []
8 Representation in functional programming issub as bs = and (map (\ x -> elem x bs) as) isclos as = issub (as >>= jump) as closure qs = let qs = qs >>= jump in if issub qs qs then qs else closure (union qs qs )
9 Representation in functional programming next a A elem a "+-" = [B] next a B elem a "023456789" = [B,E] next a C elem a "023456789" = [D] next a D elem a "023456789" = [D] next. B = [C] next. E = [D] next = [] run [] q = closure [q] run (a:x) q = closure [q] >>= next a >>= run x
20 Representation in functional programming We can prove by induction on x that run x q is always -closed The main Lemma is that any union of -closed sets is a set which is -closed
2 Eliminating -Transitions We define then the DFA D = (Q D, Σ D, δ D, q D, F D ) where Q D is the set of -closed subsets of Q Σ D = Σ δ D (X, a) = ECLOSE( (X, a)) q D = ECLOSE({q 0 }) F D = {X Q D X F } Lemma: For any x Σ we have ˆδ(q 0, x) = ˆ δ D (q D, x) Theorem: L(E) = L(D) Proof: We have x L(E) iff ˆδ(q 0, x) F iff ˆδ(q 0, x) F D iff δˆ D (q D, x) F D iff x L(D). We use the Lemma to replace ˆδ(q 0, x) by δ ˆ D (q D, x)
22 Eliminating -Transitions Similar construction as for building a DFA from a NFA but now we close at each steps For the example of decimal numbers we get the following automaton 0,,...,9 0,,...,9 {A, B} +, {B} 0,,...,9 {B, E} {C, D} 0,,...,9 {C} 0,,...,9 {D} 0,,...,9 where the state is not represented Once again, we get this program mechanically!
23 Representation in functional programming pnext a qs = closure (qs >>= next a) prun [] qs = qs prun (a:x) qs = prun x (pnext a qs) run x q = prun x (closure [q])
24 NFA as labelled graphs A NFA A = (Q, Σ, δ, q 0, F) can be seen as a labelled graph a q q2 iff q 2 δ(q ) We define also, for x Σ x q q2 by induction on x If x = this means q = q 2 a If x = ay this means that there exists q Q such that q q and q y q 2 We have q x q2 iff q 2 ˆδ(q, x) L(A) = {x Σ ( q F) q 0 x q}
25 The Product Construction on NFA Given A = (Q, Σ, δ, q, F ) and A 2 = (Q 2, Σ, δ 2, q 2, F 2 ) two NFAs with the same alphabet Σ we define the product A = A A 2 as the set of state is Q Q 2 δ((r, r 2 ), a) = δ (r, a) δ 2 (r 2, a). In this way (r, r 2 ) a (s, s 2 ) iff both r a s and r 2 a s2. (r, r 2 ) is accepting iff r F and r 2 F 2 the initial state is (q, q 2 ) Lemma: (r, r 2 ) x (s, s 2 ) iff r x s and r 2 x s2 Proposition: L(A A 2 ) = L(A ) L(A 2 )
26 Be careful! Complement of a NFA In general we don t have L(A ) = Σ L(A) if A = (Q, Σ, δ, q 0, Q F) A = (Q, Σ, δ, q 0, F) and A is a NFA
27 Σ = {} Automata with -Transitions -NFA accepting all words of length multiple of 3 or 5 The automaton guesses the right direction, and then verifies that w is correct!
28 Eliminating -Transitions This corresponds to the NFA